package com.winning.devops.batch.writer.ch04.config;

import com.winning.devops.batch.writer.ch04.domain.Customer;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.support.MySqlPagingQueryProvider;
import org.springframework.batch.item.xml.StaxEventItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.oxm.xstream.XStreamMarshaller;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Reader Writer 配置
 *
 * @author chensj
 * @date 2020-03-07 18:11
 */
@Configuration
public class XmlFileItemConfig {

    @Autowired
    private DataSource dataSource;

    /**
     * Jdbc分页读取
     *
     * @return ItemReader
     */
    @Bean
    public ItemReader<? extends Customer> xmlFileItemDemoReader() {
        JdbcPagingItemReader<Customer> reader = new JdbcPagingItemReader<>();
        reader.setDataSource(dataSource);
        reader.setPageSize(10);
        // 数据库记录转换实体类
        reader.setRowMapper(new RowMapper<Customer>() {
            @Override
            public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {

                Customer user = new Customer();
                user.setId(rs.getInt(1));
                user.setFirstName(rs.getString(2));
                user.setLastName(rs.getString(3));
                user.setBirthday(rs.getString(4));
                return user;
            }
        });
        // 指定查询语句 SQL
        MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
        // 指定查询那些字段
        provider.setSelectClause("id,firstName,lastName,birthday");
        // 指定查询那张表
        provider.setFromClause("FROM customer");
        // 指定根据那个字段进行排序
        Map<String, Order> sortKeys = new HashMap<>(1);
        // 设置ID排序
        sortKeys.put("ID", Order.ASCENDING);
        provider.setSortKeys(sortKeys);
        // 指定数据库查询信息
        reader.setQueryProvider(provider);
        return reader;
    }


    @Bean
    public ItemWriter<? super Customer> xmlFileItemDemoWriter () throws Exception {
        StaxEventItemWriter<Customer> writer = new StaxEventItemWriter<>();
        writer.setRootTagName("customers");
        writer.setResource(new FileSystemResource("g:\\customer.xml"));
        // 解析
        XStreamMarshaller marshaller = new XStreamMarshaller();
        Map<String, Class> map = new HashMap<>();
        map.put("customer", Customer.class);
        map.put("id", Integer.class);
        map.put("firstName", String.class);
        map.put("lastName", String.class);
        map.put("birthday", String.class);
        marshaller.setAliases(map);
        writer.setMarshaller(marshaller);
        writer.afterPropertiesSet();
        return writer;
    }
}
